package com.tzinfo.tzt.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.tzinfo.tzt.config.AppDataConfig;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;

public class SharedPreferencesTools {

	private Context context;
	private String fileName = "tclient";

	public SharedPreferencesTools(Context context) {
		this.context = context;
	}

	public void setFileName(String Name) {
		this.fileName = Name;
	}

	@SuppressLint("NewApi")
	public boolean saveSetMessage(String key, Set<String> value) {
		boolean flag = false;
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		SharedPreferences.Editor edit = sharedPreferences.edit();
		edit.putStringSet(key, value);
		flag = edit.commit();
		flag = true;
		return flag;
	}

	public boolean saveMapItemMessage(String key,String mapKey, Object value) {
		Map<String, Object> map = readMapMessage(key);
		if(map==null)map=new HashMap<String, Object>();
		map.put(mapKey, value);
		return saveMapMessage(key, map);
	}
	public boolean removeMapItemMessage(String key,String mapKey) {
		Map<String, Object> map = readMapMessage(key);
		if(map==null)return false;
		map.remove(mapKey);
		return saveMapMessage(key, map);
	}
	public boolean removeMapAllItemMessage(String key) {
		Map<String, Object> map = readMapMessage(key);
		if(map==null)return true;
		map.clear();
		saveMapMessage(key, map);
		return true;
	}
	
	public boolean checkMapItemMessage(String key,String mapKey) {
		Map<String, Object> map = readMapMessage(key);
		if(map==null||map.get(mapKey)==null)return false;
		return true;
	}

	public boolean saveMapMessage(String key, Map<String, Object> map) {
		boolean flag = false;
		// 将map转换为byte[]
		ByteArrayOutputStream toByte = new ByteArrayOutputStream();
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(toByte);
			oos.writeObject(map);
			String base64String = Base64.encodeToString(toByte.toByteArray(),
					Base64.DEFAULT);
			SharedPreferences sharedPreferences = context.getSharedPreferences(
					fileName, Context.MODE_PRIVATE);
			SharedPreferences.Editor edit = sharedPreferences.edit();
			edit.putString(key, base64String);
			flag = edit.commit();
			flag = true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("IOException", e.getMessage());
		} finally {
			try {
				oos.close();
				toByte.close();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}

		return flag;
	}

	public Object readMapItemMessage(String Key,String mapKey) {
		Map<String, Object> map = readMapMessage(Key);
		return map.get(mapKey);
		
	}
	public Map<String, Object> readMapMessage(String Key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		String str = sharedPreferences.getString(Key, null);
		Map<String, Object> map = null;

		byte[] base64_byte =null;
		ByteArrayInputStream byteArrayInputStream = null;
		ObjectInputStream objectInputStream = null;
		try {
			base64_byte=Base64.decode(str, Base64.DEFAULT);
			byteArrayInputStream = new ByteArrayInputStream(base64_byte);
			objectInputStream = new ObjectInputStream(byteArrayInputStream);
			map = (Map<String, Object>) objectInputStream.readObject();
		} catch (Exception e) {
			return null;
		} finally {
			try {
				objectInputStream.close();
				byteArrayInputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}

		return map;
	}

	public boolean saveSetStringMessage(String key, String value) {
		boolean flag = false;
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		SharedPreferences.Editor edit = sharedPreferences.edit();
		String nowString = sharedPreferences.getString(key, "");
		nowString = toRegex(value + ",", nowString, "");
		nowString = value + "," + nowString;
		edit.putString(key, nowString);
		flag = edit.commit();
		flag = true;
		return flag;
	}

	public boolean clearSetStringMessage(String key) {
		boolean flag = false;
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		SharedPreferences.Editor edit = sharedPreferences.edit();
		edit.remove(key);
		flag = edit.commit();
		flag = true;
		return flag;
	}

	public Set<String> readSetStringMessage(String Key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		String str = sharedPreferences.getString(Key, null);
		Set<String> result = null;
		String[] strs = null;
		if (str != null && !"".equals(str)) {
			result = new HashSet<String>();
			strs = str.split(",");
			for (int i = 0; i < strs.length; i++) {
				result.add(strs[i]);
			}
		}
		return result;
	}

	/**
	 * 去掉字符串中匹配 的字符串
	 * 
	 * @author zhujie
	 * @return String regex 要替换的内容 value 字符串 state 替换的内容变成什么
	 */
	public static String toRegex(String regex, String value, String state) {
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(value);
		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			m.appendReplacement(sb, state);
		}
		m.appendTail(sb);
		return sb.toString();
	}

	public boolean saveMessage(String key, String value) {
		boolean flag = false;
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		SharedPreferences.Editor edit = sharedPreferences.edit();
		edit.putString(key, value);
		flag = edit.commit();
		return flag;
	}

	public boolean saveMessage(String key, boolean value) {
		boolean flag = false;
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		SharedPreferences.Editor edit = sharedPreferences.edit();
		edit.putBoolean(key, value);
		flag = edit.commit();
		return flag;
	}

	/**
	 * 
	 * @param Key
	 * @return boolean
	 */
	public boolean readBooleanMessage(String Key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		return sharedPreferences.getBoolean(Key, true);
	}

	public boolean readBooleanMessage(String Key, boolean flag) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		return sharedPreferences.getBoolean(Key, flag);
	}

	@SuppressLint("NewApi")
	public Set<String> readSetMessage(String Key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		return sharedPreferences.getStringSet(Key, null);
	}

	public String readMessage(String Key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				fileName, Context.MODE_PRIVATE);
		return sharedPreferences.getString(Key, "");
	}

}